package huang.com.android.intentreceiver;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class IntentReceiver extends AppCompatActivity {
    private Button myButton = null;
    private final String nobody = "Mr.nobody";

    private Button registerButton = null;
    private Button unregisterButton = null;
    private SMSReceiverActivity smsReceiver = null;

    private static final String SMS_ACTION = "android.provider.Telephony,SMS_RECEIVED";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_receiver);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        myButton = (Button) findViewById(R.id.myButton);
        myButton.setOnClickListener(new muButtonListener());

        registerButton = (Button) findViewById(R.id.register);
        registerButton.setOnClickListener(new RegisterReceiverListener());
        unregisterButton = (Button) findViewById(R.id.unregister);
        unregisterButton.setOnClickListener(new UnregisterReceiverListener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class muButtonListener implements View.OnClickListener {
         @Override
         public void onClick(View v) {
             Intent intent = new Intent(nobody);
             //在Intent对象当中添加一个键值对
             intent.putExtra("testIntent","11111111");
             sendBroadcast(intent);
         }
     }

    class RegisterReceiverListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            //生成一个BroiadcastReveiver对象
            smsReceiver = new SMSReceiverActivity();

            IntentFilter filter = new IntentFilter();

            filter.addAction(SMS_ACTION);

            IntentReceiver.this.registerReceiver(smsReceiver,filter);

        }
    }

    class UnregisterReceiverListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            //解除BroadCastReveiver对象的注册
            IntentReceiver.this.unregisterReceiver(smsReceiver);
        }
    }
}
